/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import DAO.ChaveDAO;
import DAO.ClassificacaoGeralDAO;
import DAO.TimeDAO;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.ClassificacaoGeral;
import model.Mensagem;



/**
 *
 * @author Alberto
 */
public class Controller_ClassificacaoGeral extends HttpServlet {

    public void listar(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        ArrayList<ClassificacaoGeral> obj = new ArrayList<ClassificacaoGeral>();

        Gson objJson = new Gson();

        try {

            ClassificacaoGeralDAO classificacaoGeralDAO = new ClassificacaoGeralDAO();

            obj = classificacaoGeralDAO.abastecerChave(Integer.parseInt(request.getParameter("id_chave1")), Integer.parseInt(request.getParameter("id_chave2")));
            out.print(objJson.toJson(obj));

        } catch (Exception e) {

            System.out.println("Erro na servlet ao listar os ganhadores das chaves" + e.getStackTrace());

        } finally {
            out.close();
        }

    }

    public void alterar(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        ArrayList<Object> arrobj = new ArrayList<Object>();
        Gson objJson = new Gson();
        Mensagem msg = new Mensagem();

        try {
            ClassificacaoGeral classificacaoGeral = new ClassificacaoGeral();
            ClassificacaoGeralDAO classificacaoGeralDAO = new ClassificacaoGeralDAO();
            TimeDAO timeDAO = new TimeDAO();
   

           
            classificacaoGeral.setId(Integer.parseInt(request.getParameter("id")));
            System.out.println("O parametro nome_time é: "+ request.getParameter("nome_time"));
            classificacaoGeral.setTime(timeDAO.buscarPorNome(request.getParameter("nome_time")));
           

            classificacaoGeralDAO.atualizar(classificacaoGeral);

            msg.setMensagem("Registro atualizado com sucesso!!");

            arrobj.add(msg);
            out.print(objJson.toJson(arrobj));

        } catch (Exception e) {

            //perceba que a mensagem é gerada na controladora
            System.out.println("Erro ao alterar o Classificação " + e.getMessage());

            out.print(objJson.toJson(arrobj));

        } finally {
            out.close();
        }

    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int oper = Integer.parseInt(request.getParameter("oper"));
        try {
            switch (oper) {

                case 1:
                    listar(request, response);
                    break;

                case 2:
                    alterar(request, response);
                    break;

            }

        } catch (Exception e) {
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold> 
}
